<?php


class tx_zlib_viewhelper_url extends tx_zlib_abstract_viewhelper
{

    /**
	 * Use absolute URL (eg. http://www.mysite.com/index.php?id=14)
	 *
	 * @var boolean
     */
    private $useAbsoluteUrl = false;

	/**
	 * Generates an URL to the current page (the caller page) using PI vars.
	 * This is the shortform of url()->thisPagePIvars().
	 *
	 * If no arguments are passed, returns a tx_zlib_viewhelper_url object.
	 *
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string|tx_zlib_viewhelper_url
	 */
	public function url(array $overrulePIvars=array(), $cache=true, $clearAnyway=true, $altPageId=0)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->thisPagePIvars($overrulePIvars, $cache, $clearAnyway, $altPageId);
	}

	/**
	 * Generates an URL to a certain page.
	 *
	 * @param integer $id
	 * @param array $urlParameters
	 * @return string The generated URL
	 */
	public function toPage($id, array $urlParameters=array())
	{
		$target = '';
		$url = $this->getPi()->pi_getPageLink($id, $target, $urlParameters);
        return $this->buildUrl($url);
	}

	/**
	 * Generates an URL to a certain page using PI vars.
	 *
	 * @param integer $id
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @return string The generated URL
	 */
	public function toPagePIvars($id, array $overrulePIvars=array(), $cache=true, $clearAnyway=true)
	{
		$url = $this->getPi()->pi_linkTP_keepPIvars_url($overrulePIvars, $cache, $clearAnyway, $id);
        return $this->buildUrl($url);
	}

	/**
	 * Generates an URL to the current page (the caller page).
	 *
	 * @param array $urlParameters
	 * @return string The generated URL
	 */
	/*
	public function thisPage(array $urlParameters=array())
	{
		$id = $GLOBALS['TSFE']->page['uid'];
		$target = '';
		return $this->getPi()->pi_getPageLink($id, $target, $urlParameters);
	}
	*/

	/**
	 * Generates an URL to the current page (the caller page).
	 *
	 * @param array $urlParameters
	 * @param boolean $cache
	 * @param integer $altPageId
	 * @return string The generated URL
	 */
	public function thisPage($urlParameters=array(), $cache=true, $altPageId=0)
	{
	    $pi = $this->getPi();
		$conf=array();
		if(empty($altPageId)) {
    		$conf['useCacheHash'] = $pi->pi_USER_INT_obj ? 0 : $cache;
    		$conf['no_cache'] = $pi->pi_USER_INT_obj ? 0 : !$cache;
		} else {
    		$conf['useCacheHash'] = intval($cache);
    		$conf['no_cache'] = intval($cache);
		}
		$conf['parameter'] = $altPageId ? $altPageId : ($pi->pi_tmpPageId ? $pi->pi_tmpPageId : $GLOBALS['TSFE']->id);
		$conf['additionalParams'] = $pi->conf['parent.']['addParams'].t3lib_div::implodeArrayForUrl('',$urlParameters,'',1).$pi->pi_moreParams;
		$url = $pi->cObj->typoLink_URL($conf);
        return $this->buildUrl($url);
	}

	/**
	 * Generates an URL to the current page using PI vars (the caller page).
	 *
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string The generated URL
	 */
	public function thisPagePIvars(array $overrulePIvars=array(), $cache=true, $clearAnyway=true, $altPageId=0)
	{
		$url = $this->getPi()->pi_linkTP_keepPIvars_url($overrulePIvars, $cache, $clearAnyway, $altPageId);
        return $this->buildUrl($url);
	}

	/**
	 * @param boolean $flag
	 * @return tx_zlib_viewhelper_url
	 */
	public function setUseAbsoluteUrl($flag)
	{
	    $this->useAbsoluteUrl = (bool)$flag;
	    return $this;
	}

    /**
     * @param string $url
     * @return string
     */
    private function buildUrl($url)
    {
        if($this->useAbsoluteUrl) {
            return t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $url;
        }
        return $url;
    }

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_url.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_url.php']);
}
